package org.hxzon.demo.netty;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;

public class FileDownloadDecoder extends FrameDecoder {
	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) {
		int len = 0;
		if (buffer.readableBytes() < 4) {
			return null;
		} else {
			buffer.markReaderIndex();
			len = buffer.readInt();
		}
		System.out.println(buffer.readableBytes());
		if (buffer.readableBytes() < len) {
			buffer.resetReaderIndex();
			return null;
		}
		return buffer.readBytes(len);
	}
}